package nave.android.game;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.Point2D;

import android.graphics.Rect;

public class Player extends AnimatedObject
{
	Rect rectangle;
	public STATE statePlayer = STATE.NO_CHAO;
	
	private Point2D velocity;
	private final float constantePulo = 1;
	private float velocityPulo = 0;
	private float gravidade = 1;
	private boolean podeCorda = true;
	private Rect rectCorda, rectEscada;
	
	public Player(Point2D position, Point2D size)
	{
		super(null, position, size, new Point2D.Double(0,0), new Point2D.Double(0,0), 0, new Point(10,10), false);
	}
	
	public void update(Rect[] paredes, Rect[] chao, Rect[] corda, Rect[] escada)
	{
		switch (this.statePlayer) 
		{
			case NA_CORDA:
				this.MovimentoCorda();
				break;
				
			case NO_CHAO:
				this.MovimentoChao(paredes, escada);
				break;
				
			case NA_ESCADA:
				this.MovimentoEscada(paredes);
				break;
				
			case PULANDO:
				this.MovimentoPulando(chao, corda);
				break;
				
			case MORTO:
				//ele morre
				break;
				
		}
		
	}

	public void draw(Graphics2D g2d)
	{
		
	}

	public void MovimentoChao(Rect[] paredes, Rect[] escada)
	{
		podeCorda = true;
		
		if(/*input esquerda*/true)
		{
			this.posicao = new Point2D.Double(this.posicao.getX() - velocity.getX(), this.posicao.getY());
			
			for (int i = 0; i < paredes.length; i++) 
			{
				if(CollisionManager.getInstance().CheckCollisionRect(this.AtualizarRetangulo(), paredes[i]))
				{
					this.posicao = new Point2D.Double(paredes[i].right, this.posicao.getY());
					break;
				}
			}
		}
		else if(/*input direita*/false)
		{
			this.posicao = new Point2D.Double(this.posicao.getX() + velocity.getX(), this.posicao.getY());
			
			for (int i = 0; i < paredes.length; i++) 
			{
				if(CollisionManager.getInstance().CheckCollisionRect(this.AtualizarRetangulo(), paredes[i]))
				{
					this.posicao = new Point2D.Double(paredes[i].left - this.tamanho.getX(), this.posicao.getY());
					break;
				}
			}
		}
		
		if(/*input de PULO*/true)
		{
			this.velocityPulo = this.constantePulo;
			this.statePlayer = STATE.PULANDO; 
		}
		if(/*input CIMA*/ true)
		{
			if(TesteColisaoEscada(escada))
			{
				this.statePlayer = STATE.NA_ESCADA;
			}
		}
		if(/*input BAIXO*/ true)
		{
			for (int i = 0; i < escada.length; i++) 
			{
				Rect r = new Rect(escada[i].left,
						(int)(escada[i].top - this.tamanho.getY() - 10),
						escada[i].right,
						escada[i].bottom);
				
				if(CollisionManager.getInstance().OnCollisionGameObjectAndRect(this, r))
				{
					this.statePlayer = STATE.NA_ESCADA;
					this.rectEscada = escada[i];
					this.posicao = new Point2D.Double(escada[i].centerX() - this.tamanho.getX()/2, this.posicao.getY());	
				}
				
			}
		}
	}
	
	public void MovimentoEscada(Rect[] paredes)
	{
		if(/*input CIMA*/true)
		{
			this.posicao = new Point2D.Double(this.posicao.getX(), this.posicao.getY() + this.velocity.getY());
			
			if(this.posicao.getY() <= this.rectEscada.top - this.tamanho.getY())
			{
				this.statePlayer = STATE.NO_CHAO;
			}
		}
		else if(/*input BAIXO*/false)
		{
			this.posicao = new Point2D.Double(this.posicao.getX() + velocity.getX(), this.posicao.getY());
			
			
			if(this.AtualizarRetangulo().bottom >= this.rectEscada.bottom)
				this.posicao = new Point2D.Double(this.posicao.getX(), this.rectEscada.bottom - this.tamanho.getY());
		}
		
		if(this.AtualizarRetangulo().bottom >= this.rectEscada.bottom - 10)
		{
			this.statePlayer = STATE.PULANDO;
		}
	}
	
	public void MovimentoCorda()
	{
		if(/*input CIMA*/true)
		{
			this.posicao = new Point2D.Double(this.posicao.getX(), this.posicao.getY() - this.velocity.getY());
			
			if(this.posicao.getY() <= this.rectEscada.top - this.tamanho.getY())
			{
				this.statePlayer = STATE.PULANDO;
			}
		}
		else if(/*input BAIXO*/false)
		{
			this.posicao = new Point2D.Double(this.posicao.getX(), this.posicao.getY() + velocity.getY());
			
			if(this.AtualizarRetangulo().bottom >= this.rectEscada.bottom)
				this.posicao = new Point2D.Double(this.posicao.getX(), this.rectEscada.bottom - this.tamanho.getY());
		}
		
		if(/* input esquerda*/true || true/*input direita*/)
		{
			if(this.AtualizarRetangulo().bottom >= this.rectEscada.bottom - 10)
			{
				this.statePlayer = STATE.PULANDO;
				podeCorda = false;
			}
		}
	}
	
	public void MovimentoPulando(Rect[] chao, Rect[] corda)
	{
		this.velocityPulo -= this.gravidade;
		
		this.posicao = new Point2D.Double(this.posicao.getX(), this.posicao.getY() + this.velocityPulo);
		
		if(this.velocityPulo <= 0)
		{
			for (int i = 0; i < chao.length; i++) 
			{
				if(/*TESTA COLISAO COM CHAO*/ true)
				{
					this.posicao = new Point2D.Double(
							this.posicao.getX(),
							chao[i].top - this.tamanho.getY()
							);
					
					this.statePlayer = STATE.NO_CHAO;
					break;
				}
			}
		}
		if(TesteColisaoCorda(corda))
		{
			this.velocityPulo = 0;
			this.statePlayer = STATE.NA_CORDA;
		}
	}
	
	public boolean TesteColisaoCorda(Rect[] corda)
	{
		for (int i = 0; i < corda.length; i++) 
		{
			if(CollisionManager.OnCollisionGameObjectAndRect(this, corda[i]))
			{
				this.rectCorda = corda[i];
				this.posicao = new Point2D.Double(corda[i].left - this.tamanho.getX()/2, this.posicao.getY());		
				return true;
			}
		}
		return false;
	}
	
	public boolean TesteColisaoEscada(Rect[] escada)
	{
		for (int i = 0; i < escada.length; i++) 
		{
			if(CollisionManager.OnCollisionGameObjectAndRect(this, escada[i]))
			{
				this.rectEscada = escada[i];
				this.posicao = new Point2D.Double(escada[i].centerX() - this.tamanho.getX()/2, this.posicao.getY());		
				return true;
			}
		}
		return false;
	}
	
	public Rect AtualizarRetangulo()
	{
		return new Rect(
				(int)this.posicao.getX(),
				(int)this.posicao.getY(),
				(int)this.tamanho.getX(),
				(int)this.tamanho.getY()
				);
	}

	enum STATE
	{
		PULANDO,
		NA_CORDA,
		NA_ESCADA,
		NO_CHAO,
		MORTO
	}
	

	

}
